//
//  JKStoreProductListVC.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/29.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKStoreProductListVC.h"

// vc
#import "JKStoreProductDetailVC.h"

// view
#import "JKViewSearch.h"                    // 搜索
#import "JKViewFriendsSegment.h"            // 分类选择

// cell
#import "JKCellStoreProductListItem.h"

// api
#import "ByApiStore.h"


@interface JKStoreProductListVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) JKViewFriendsSegment *segment;

@property (nonatomic, assign) NSInteger page_index;
@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation JKStoreProductListVC
{
    
}
#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];

    [[self.view viewWithTag:888] removeFromSuperview];
    
    [self createNaviItems];
    
    [self addColorLeftBackBarButtonItem];
    
    [self createTableView];
    
    _page_index = 1;
    [self.table.mj_header beginRefreshing];
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self setNaviAttributeWithTranslucent:false stateBarLightContent:false];
}

#pragma mark - 请求数据
- (void)getData {
    
    [self.table.mj_header endRefreshing];
    
    ByNetParamsModel *params = [ByNetParamsModel new];
    params.page_index = [NSString stringWithFormat:@"%li", _page_index];
    params.page_size = @"10";
    params.type = [NSString stringWithFormat:@"%li", _type];
    [ByApiStore jk_getStoreList:params success:^(ByNetBaseResult *result) {
        
        NSDictionary *dic = result.data;
        NSInteger total = [dic[@"count"] integerValue];
        
        if (_page_index == 1) {
            self.items = [NSMutableArray new];
        }
        
        [self.items addObjectsFromArray:dic[@"list"]];
        
        if (self.items.count == total) {
            [self.table.mj_header endRefreshing];
            [self.table.mj_footer endRefreshingWithNoMoreData];
        }
        else {
            [self.table.mj_header endRefreshing];
            [self.table.mj_footer endRefreshing];
        }
        
        [self.table reloadData];

    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
        [self.table.mj_header endRefreshing];
        [self.table.mj_footer endRefreshing];
    }];
    

}

#pragma mark - 创建视图
- (void)createNaviItems {
    // 搜索
    JKViewSearch *searchView = [[JKViewSearch alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.7 - 40, 30)];
    searchView.searchBlock = ^(NSString *searchText) {
    };
    self.navigationItem.titleView = searchView;
}
- (void)createTableView {
    
    _segment = [JKViewFriendsSegment new];
    _segment.segments = @[@"综合排序", @"好评优先", @"价位优先"];
    [self.view addSubview:_segment];
    [_segment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.height.equalTo(@60);
    }];
    __weak typeof(self) wself = self;
    _segment.selectBlock = ^(NSInteger index) {
//        wself.type = index + 1;
        [wself.table.mj_header beginRefreshing];
    };
    
    self.table = [[UITableView alloc] init];
    self.table.backgroundColor = [UIColor whiteColor];
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(60, 0, 0, 0)).priorityMedium();
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-0);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        }
    }];
    

    
    
    [self.table registerClass:[JKCellStoreProductListItem class] forCellReuseIdentifier:@"cell"];
    
    
    self.table.mj_header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        self.page_index = 1;
        [self getData];
    }];
    self.table.mj_footer = [ByCoreRefreshGifFooter footerWithRefreshingBlock:^{
        self.page_index ++;
        [self getData];
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.items.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JKCellStoreProductListItem *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.dic = self.items[indexPath.section];
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return UIView.new;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    view.backgroundColor = TABLE_COLOR;
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JKStoreProductDetailVC *vc = [JKStoreProductDetailVC new];
    [self by_normalPush:vc hasNavi:true];
}


#pragma mark - 懒加载
- (NSMutableArray *)items {
    
    if (!_items) {
        _items = [NSMutableArray new];
    }
    return _items;
}

@end
